//
//  ShopInfoViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/5.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "ShopInfoViewController.h"
#import "ShopInfoTableViewCell.h"
#import "ShopInfoModel.h"

@interface ShopInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;
@property (nonatomic,strong) SDCycleScrollView *sdcView;
@property (nonatomic,strong) UILabel *infoLabel;
@property (nonatomic,strong) UIImageView *imgView;

@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;

@property (nonatomic,strong) NSDictionary *model;

@property (nonatomic,strong) UIButton *attentionBtn;

@end

//网络
@implementation ShopInfoViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"s_id":self.sId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"StoreMobile/selectbysId.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] integerValue] == 0) {
            self.model = responseObject[@"storeDetails"];
            self.sdcView.imageURLStringsGroup = @[[NSURL URLOffsetIPWithString:self.model[@"picture"]].absoluteString];
            [self.imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:self.model[@"s_logo"]] placeholderImage:[UIImage imageNamed:@"image"]];
            [self.view addSubview:self.tableView];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

- (void)getFollowSid{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"sId":self.sId,
                                 @"auId":[UserModel sharedInstanced].userId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"UserStoreFollow/followSid.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
//            self.attentionBtn.selected = YES;
            self.attentionBtn.selected = YES;
        }else{
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 关注店铺
 */
- (void)getfollow{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId,
                                 @"s_id":self.sId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"UserStoreFollow/follow.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
           self.attentionBtn.selected = YES;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 取消关注店铺
 */
- (void)getTakeOff{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"auId":[UserModel sharedInstanced].userId,
                                 @"s_id":self.sId};
    [[AFAppDotNetAPIClient sharedClient] POST:@"UserStoreFollow/takeOff.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            self.attentionBtn.selected = NO;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation ShopInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"店铺详情";
//    [self getData];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:({
        _attentionBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 24)];
        [_attentionBtn setTitle:@"关注" forState:UIControlStateNormal];
        [_attentionBtn setTitle:@"已关注" forState:UIControlStateSelected];

        [_attentionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _attentionBtn.titleLabel.font = DEF_FontSize_12;
        _attentionBtn.layer.masksToBounds = YES;
        _attentionBtn.layer.cornerRadius = 3;
        _attentionBtn.layer.borderWidth = .5;
        _attentionBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        [_attentionBtn addTarget:self action:@selector(attentionEvent:) forControlEvents:UIControlEventTouchUpInside];
        _attentionBtn;
        
    })];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

#pragma mark - Event

- (void)attentionEvent:(UIButton *)sender{
//    sender.selected = !sender.selected;
    if (!sender.selected) {
        NSLog(@"关注");
        [self getfollow];
    }else{
        NSLog(@"取消关注");
        [self getTakeOff];
    }
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ShopInfoTableViewCell class])];
    cell.model = self.model;
    cell.state = (ShopInfoState)indexPath.row;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? 100 : 128;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[ShopInfoTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ShopInfoTableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 115/WIDTH_5S_SCALE + 72)];
        
        UIView *subView= [[UIView alloc] initWithFrame:CGRectMake(0, 115/WIDTH_5S_SCALE, SCREEN_WIDTH, 72)];
        subView.backgroundColor = [UIColor whiteColor];
        
        _infoLabel = [UILabel new];
        _infoLabel.numberOfLines = 0;
        _infoLabel.font = DEF_FontSize_13;
        _infoLabel.frame = CGRectMake(CGRectGetMaxX(_imgView.frame)+5, 0, SCREEN_WIDTH - CGRectGetMaxX(self.imgView.frame) - 60, 72);
        
        
        NSMutableAttributedString *attStr1 = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@\n\n",self.model[@"s_name"]] attributes:nil];
        for (int i = 0; i < 5; i ++) {
            if (i < [self.model[@"user_mark"] integerValue]) {
                NSTextAttachment *seeAtt1 = [NSTextAttachment new];
                seeAtt1.bounds = CGRectMake(0, -2, 15, 15);
                seeAtt1.image = [UIImage imageNamed:@"xin_sel"];
                [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
            }else{
                NSTextAttachment *seeAtt1 = [NSTextAttachment new];
                seeAtt1.bounds = CGRectMake(0, -2, 15, 15);
                seeAtt1.image = [UIImage imageNamed:@"xin"];
                [attStr1 appendAttributedString:[NSAttributedString attributedStringWithAttachment:seeAtt1]];
            }
        }
        _infoLabel.attributedText = attStr1;
        
        [_headerView addSubview:_sdcView];
        [_headerView addSubview:subView];
        [subView addSubview:_imgView];
        [subView addSubview:_infoLabel];
    }
    return _headerView;
}

- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.layer.masksToBounds = YES;
        _imgView.frame = CGRectMake(12, 0, 57, 57);
        _imgView.center = CGPointMake(_imgView.center.x, 36);
        _imgView.backgroundColor = getColor(@"f1f2f6");
    }
    return _imgView;
}

- (SDCycleScrollView *)sdcView{
    if (!_sdcView) {
        _sdcView = [[SDCycleScrollView alloc ]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 115/WIDTH_5S_SCALE)];
        _sdcView.backgroundColor = getColor(@"f1f2f6");
    }
    return _sdcView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[];
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

- (void)setSId:(NSString *)sId{
    _sId = sId;
    [self getData];
    [self getFollowSid];
}

@end
